import { RouteLocation, RouteLocationNormalized } from 'vue-router'
import config from '/@/config/app'

/**
 * 处理面包屑
 * @param route
 */
export function handleBreadcrumb(route: RouteLocation): Array<object> {
  let breadcrumbs: Array<object> = [
    {
      icon: 'HomeOutlined',
      title: '首页',
      url: '/'
    }
  ]

  if (route.fullPath !== '/') {
    const matched = route.matched
    const matchedList: Array<any> = []

    for (let i = 0; i < matched.length; i++) {
      const item = matched[i]
      matchedList.push({
        icon: item.meta.icon,
        title: item.meta.title,
        url: item.path
      })

      if (item.meta.one) {
        break
      }
    }

    breadcrumbs = breadcrumbs.concat(matchedList)
  }

  return breadcrumbs
}

/**
 * 设置页面标题
 * @param to router
 */
export function setPageTitle(to: RouteLocationNormalized) {
  const sys = '老虎管理系统'
  const title = to.meta.title
  document.title = title ? `${title} - ${sys}` : sys
}

/**
 * 默认主题
 */
export function getDefaultTheme() {
  const theme = window.localStorage.getItem('default-theme')
  const defaultTheme = theme ? +theme : 1
  config.theme.defaultTheme = theme ? +theme : 1
  return defaultTheme ? config.theme.colorList.find(item => item.id === +defaultTheme) : {}
}
